<?php
require_once(dirname(__FILE__)."/../../libs/dbclass/dbconnect.php");
require_once(dirname(__FILE__)."/../../libs/string/libstr.php");
require_once(dirname(__FILE__)."/../../admin/libs/config_class.php");
$mc=new libstr();
$cf=new config();
class dataclass{
//获得主类别
function getclass($cataid=0,$tb="product_class"){
	global $db;
	global $mc;
	$sql="select ID,classname,classorder,rootid,(select count(b.ID) as tt from ".$tb." as b where b.pid=t.ID) as tj  from ".$tb." as t where  t.pid='".$cataid."' order by classorder asc";
	$getrs=$db->ds($sql);
	return $getrs;
}

//获得产品类别
function getClassList($cataid=0,$tb="product_class"){
	global $db;
	global $mc;
	$classDetail=$this->getcataiddetail($cataid,$tb); 
	$classNum=$classDetail['tj'];  //子项目条数
	if($classNum==0)$cataid=$classDetail['pid'];  
	$sql="select ID,classname,classorder,rootid,(select count(b.ID) as tt from ".$tb." as b where b.pid=t.ID) as tj  from ".$tb." as t where  t.pid='".$cataid."' order by classorder asc";
	$getrs=$db->ds($sql);
	return $getrs;
}
//获得类别名称id转化
function getclassname($ID,$tb="product_class"){
	global $db;
	global $mc;
	$query="select * from ".$tb." where ID='".$ID."'";
	$getrs=$db->ds($query);
	return $getrs[0]["classname"];
}
//获得类别名称rootid转化
function getclassname2($rootid="0,1,",$tb="product_class"){
	global $db;
	global $mc;
	if($mc->Ismp($rootid)=="" ||$rootid=="0"){
		return "";
	}
	if(ereg("\|",$rootid)==1){
		$root=split("\|",$rootid);
		$classname="";
		foreach ($root as $rs){
			$ids=split(",",$rs);
	$ID=$ids[count($ids)-2];
	if($classname==""){
	$classname.=$this->getclassname($ID,$tb);
	//$classname.=$rs;
	}else{
		$classname.=",".$this->getclassname($ID,$tb);
		  
			//$classname.=$rs;
	}
}
	}else{
	$ids=split(",",$rootid);
	$ID=$ids[count($ids)-2];
	$classname=$this->getclassname($ID,$tb);
	}
	return $classname;	
}


//判断是否有子集
function myck($id,$tb="product_class")
{
	global $db;
	$rs1=$db->ds("select * from ".$tb." where pid='".trim($id)."'");
	if(count($rs1)>0){
		return true;
	}else{
		return false;
	}
}
//获得类别详细
function getcataiddetail($cid,$tb="product_class"){
    global $db;
	$getrs=$db->dvstr("select * from ".$tb." where ID='".$cid."'");
	$nx=$db->ds("select count(ID) as tj from ".$tb." where pid='".$cid."'");
	$getrs["tj"]=$nx[0]["tj"];
	return $getrs;

}
//获取列表id
function getcataid($rootid){
    
if($rootid!=""){
	$dd=split(",",$rootid);
	$i=count($dd)-2;
	return $dd[$i];
}
return 0;
}
//获得所有自定义属性
function getshuxingtype(){
	global $db;
	global $mc;
	$sxarr=$db->ds("select * from shuxing_type order by classorder asc");
	return $sxarr;
}
//获得属性详细数组
function getshuxing($name){
	global $db;
	global $mc;
	$sxarr=array();
if($mc->Ismp($name)!=""){
	$tb=$name."_class";
	$sxarr=$db->ds("select * from ".$tb." order by classorder asc");
	
}
	return $sxarr;
}

//获得产品列表
function getproductarr($string="*",$cataid="",$guanlian="",$keyword="",$shuxing=array(),$userno="",$item="topicname",$order="id",$sj="desc",$pagesize=10,$page=1,$tese="")
	{
		global $mc,$db;
		$_string="*";
		if($string!="")
		{
			$_string=$string;
		}		
		$_rootid="";
		
		if($cataid!="")
		{ 
			if($mc->isnum($cataid)=="true"){
			$rootid=$db->myrootid("product_class",$cataid);
			$_rootid=" and rootid like '%".trim($rootid)."%' ";
			}
			
		}	
		$_keyword="";
		if($keyword!="")
		{
			$_keyword=" and ".$item." like '%".trim($keyword)."%' ";
		}
		if($guanlian!="")
		{
			$_guanlian=" and guanlian=".$guanlian."";
		}
		if($tese!="")
		{
			$_tese=" and tese=".$tese."";
		}
		
		//获得某个人的产品
		$_userno="";
		if($userno!="")
		{
			$_userno=" and adduserno=".$userno;
		}
		
		$_isshow="isshow>=0";	
		$_shuxing="";
	if(count($shuxing)!=0){
		while(list($key,$val)=each($shuxing)) {
			$_shuxing.=" and ".$key."='".$val."'";
		}
		}
	//最终条件
		$_where=$_isshow.$_rootid.$_keyword.$_userno.$_shuxing.$_guanlian.$_tese;
		
		$_tb="product_content";
		$_order="";
		if($order!="")
		{
			$_order=" order by ".$order." ".$sj." ";	
		}
				
		$_pagesize=$pagesize;
		$_page=$page;
					
		$totalcount_xiaoquan=$db->getcount("id",$_tb,$_where);		
		$getrs=$db->pagerds($_string,$_tb,$_where,$_order,$totalcount_xiaoquan,$_pagesize,$_page);
		$i=0;
		$sxtype=$this->getshuxingtype();
		if($totalcount_xiaoquan>0){
		foreach($getrs as $v)
		{
			$getrs[$i]=$v;
			$getrs[$i]["totalcount"]=$totalcount_xiaoquan;
			$getrs[$i]["classname"]=$this->getclassname2($getrs[$i]["rootid"]);
			$getrs[$i]["createtime"]=date("Y-m-d H:i:s",$getrs[$i]["createtime"]);
			if($getrs[$i]["pic"]!=""){
			$getrs[$i]["pics"]=split(",",$getrs[$i]["pic"]);
			$getrs[$i]["pic"]=$getrs[$i]["pics"][0];
			$getrs[$i]["thumdpic"]=$mc->getthumbpic($getrs[$i]["pic"]);
			}else{
			$getrs[$i]["pics"]=array();
			$getrs[$i]["thumdpic"]="";	
			}
			foreach ($sxtype as $sx){
				$getrs[$i][$sx["ziduan"]]=$this->getclassname2($getrs[$i][$sx["ziduan"]],$sx["ziduan"]."_class");
				//$getrs[$i][$sx["ziduan"]]=$getrs[$i][$sx["ziduan"]];
				
			}
			//转化属性
		
			$i=$i+1;
		}
		}
		return $getrs;
		
}
//获得产品详细
function getproductdetail($id){
	global $mc,$db;
	$mbshow=array();
	if($mc->Ismp($id)!=""){
	$mbshow=$db->dvstr("select * from product_content where id='".$cid."' limit 1");	
	if(count($mbshow)!=0){
	$dt=$db->dvstr("select content from product_detail where productid='".$cid."' limit 1");
	if($dt["content"]!=""){
			$mbshow["content"]=$dt["content"];
	}
	     $sxtype=$this->getshuxingtype();
		    $mbshow["classname"]=$this->getclassname2($mbshow["rootid"]);
		    $mbshow["cataid"]=$this->getcataid($mbshow["rootid"]);
			$mbshow["createtime"]=date("Y-m-d H:i:s",$mbshow["createtime"]);
			if($mbshow["pic"]!=""){
			$pics=split(",",$mbshow["pic"]);
			$i=0;
			foreach ($pics as $v){
				$mbshow["pics"][$i]["bigpic"]=$v;
				$mbshow["pics"][$i]["smpic"]=$mc->getthumbpic($v);
				$i++;
			}
			
			
			$mbshow["pic"]=$pics[0];
			$mbshow["thumdpic"]=$mc->getthumbpic($mbshow["pic"]);
			}else{
			$mbshow["pics"]=array();
			$mbshow["thumdpic"]="";	
			}
			foreach ($sxtype as $sx){
				$mbshow[$sx["ziduan"]]=$this->getclassname2($mbshow[$sx["ziduan"]],$sx["ziduan"]."_class");
				//$getrs[$i][$sx["ziduan"]]=$getrs[$i][$sx["ziduan"]];
				
			}
	}
	}
	return $mbshow;
}
//更新浏览次数
function updatehit($id,$tb="product_content"){
		global $mc,$db;
		if($mc->Ismp($id)!=""){
		$db->cmd("update ".$tb." set hit=hit+1 where id='".$id."'");	
		return true;
		}
		
}

/*获得文章列表
*$string="*";  要获取的字段，默认全部，非必需填
*$cataid="";   主类别id，非必需填
*$tuijie="tj"; 文章性质,非必需填，传入后台自定义性质参数
*$keyword=""; 要查找的关键字，默认为空，非必需填
*$userno="";  添加人，默认为空
*$item="topicname";//要查找的内容，配合keyword是使用，默认为查找标题，非必需填
*$order="id"; 排序，默认为id,可以是updatetime,createtime等
*$sj="desc"; 升降序，默认是降序,asc为升序
*$pagesize=10; 每页显示,默认为10
*$page=1; 页码，默认为
*/
function getArticleArr($string="*",$cataid="",$tuijie="",$keyword="",$userno="",$item="topicname",$order="id",$sj="desc",$pagesize=10,$page=1)
	{
		global $mc,$db;
		$_string="*";
		if($string!="")
		{
			$_string=$string;
		}		
		$_rootid="";
		
		if($cataid!="")
		{ 
			if($mc->isnum($cataid)=="true"){
			$rootid=$db->myrootid("article_class",$cataid);
			$_rootid=" and rootid like '%".trim($rootid)."%' ";
			}
			
		}	
		$_keyword="";
		if($keyword!="")
		{
			$_keyword=" and ".$item." like '%".trim($keyword)."%' ";
		}
	
		//获得某个人的产品
		$_userno="";
		if($userno!="")
		{
			$_userno=" and adduserno=".$userno;
		}
			$_tuijie="";
		if($tuijie!="")
		{
			$_tuijie=" and tuijie like  '%".trim($tuijie)."%' ";
		}
		
		
		$_isshow="isshow>=0";	
	//最终条件
		$_where=$_isshow.$_rootid.$_keyword.$_tuijie;
		
		$_tb="article_content";
		$_order="";
		if($order!="")
		{
			$_order=" order by ".$order." ".$sj." ";	
		}
				
		$_pagesize=$pagesize;
		$_page=$page;
					
		$totalcount_xiaoquan=$db->getcount("id",$_tb,$_where);		
		$getrs=$db->pagerds($_string,$_tb,$_where,$_order,$totalcount_xiaoquan,$_pagesize,$_page);
			$i=0;
	
		if($totalcount_xiaoquan>0){
		foreach($getrs as $v)
		{
			$getrs[$i]=$v;
			$getrs[$i]["totalcount"]=$totalcount_xiaoquan;
			$getrs[$i]["classname"]=$this->getclassname2($getrs[$i]["rootid"]);
			$getrs[$i]["createtime"]=date("Y-m-d H:i:s",$getrs[$i]["createtime"]);
			
			//转化属性
		
			$i=$i+1;
		}
		}
		return $getrs;
		
}
	//获得文章详细
	function getArticleDetail($id){
			global $mc,$db;
		$mbshow=array();
		if($mc->Ismp($id)!=""){
		$mbshow=$db->dvstr("select * from article_content where id='".$id."' limit 1");	
		$dt=$db->dvstr("select content from article_detail where articleid='".$id."' limit 1");
		if($dt["content"]!=""){
				$mbshow["content"]=$dt["content"];
		}
				$mbshow["classname"]=$this->getclassname2($mbshow["rootid"]);
				$mbshow["createtime"]=date("Y-m-d H:i:s",$mbshow["createtime"]);
		}
		return $mbshow;
	}
	//获得广告图
	function getAd($position=1,$limit=4){
			global $mc,$db;
			$ad=$db->ds("select * from img_content where position=".$position." and isshow>0 order by classorder asc limit ".$limit."");	
			return $ad;
	}
	
	

	//获得网站信息，关键字，描述等
	function getWebMessage(){
		global $mc,$db;
			$webmsg=$db->ds("select * from webmessage");	
			return $webmsg['0'];
	}
	
	/*
	* 查找友情链接
	* $order 默认正序排序
	* $isshow  默认显示
	*/
	function getFriendLink($order='asc',$isshow='1'){
		global $mc,$cf,$db;
		$condition =" where isshow=".$isshow;
		$condition.=' order by classorder '.$order;
		$info=$cf->getInfos("friendly_link",$condition);
		return $info;
	}
	
	
	/*
	* 查找单页文档
	* $id 文章id,默认为1
	*/
	function getSingleActicle($id='1'){
		global $mc,$cf,$db;
		$condition=" where id=".$id;
		$artInfo=$cf->getInfo('single_acticle',$condition);
		return $artInfo;
	}
	
	/*
	* 查找单页文档的简介
	* $id 文章id,默认为1
	*/
	function getSummary($id){
		$artInfo=$this->getSingleActicle($id);
		return $artInfo['summary'];
	}
	
	function getContactInfo(){
		global $cf;
		$info=$cf->getInfo('contact_info');
		return $info;
	}
	
	
}


?>